package org.boot.easyapi.core.util.sql;

import org.boot.easyapi.core.dao.ApiRespRepository;
import org.boot.easyapi.core.service.table.TableService;
import org.boot.easyapi.pojo.db.TableDTO;
import org.boot.easyapi.util.ObjectUtil;
import org.boot.easyapi.util.StrUtil;
import org.boot.easyapi.util.spring.SpringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class ParamsOperaBuildUtil {


    private static TableService tableService;
    private static ApiRespRepository apiRespRepository;


    static  {
        tableService = SpringUtil.getBean("easyapi-tableService", TableService.class);
        apiRespRepository = SpringUtil.getBean("easyapi-apiRespRepository", ApiRespRepository.class);
    }


    public static String getFieldsNotOnlyCamelCase(String tableName, String key) {
        //屏蔽字段
        if(StrUtil.isNotEmpty(key)){
            List<String> tableFields = tableService.getTableFields(tableName);
            String respFields = apiRespRepository.getFieldsByKey(tableName, key);
            if(StrUtil.isNotEmpty(respFields)){
                List<String> filterFields = new ArrayList<>();
                for (String tableField : tableFields) {
                    if(!respFields.contains(tableField)) filterFields.add(tableField);
                }
                return "*";
            }
        }
        return "*";
    }

    /**
     * 处理屏蔽字段
     * @param tableName
     * @param key
     * @return
     */
    public static String getFields(String tableName, String key) {
        //屏蔽字段
        if(StrUtil.isNotEmpty(key)){
            List<String> tableFields = tableService.getTableFields(tableName);
            String respFields = apiRespRepository.getFieldsByKey(tableName, key);
            if(StrUtil.isNotEmpty(respFields)){
                List<String> filterFields = new ArrayList<>();
                for (String tableField : tableFields) {
                    if(!respFields.contains(tableField)) filterFields.add(tableField);
                }
                return MappingConvertUtil.mappingFiled(filterFields);
            }
        }
        return convertAsFields(tableName);
    }

    /**
     * 映射查询字段
     * @param tableName
     * @return
     */
    public static String convertAsFields(String tableName) {
        List<String> fields = tableService.getTableFields(tableName);
        return MappingConvertUtil.mappingFiled(fields);
    }





    /**
     * 映射ExcelC查询的字段
     */
    public static String convertExcelAsFields(String tableName, Object respKey) {
        StringBuilder str = new StringBuilder();
        List<TableDTO> table = tableService.getTable(tableName);
        String respFields = "";
        if(ObjectUtil.isNotEmpty(respKey) && StrUtil.isNotEmpty(respKey.toString())){
            respFields = apiRespRepository.getFieldsByKey(tableName, respKey.toString());
        }
        for (TableDTO tableDTO : table) {
            if(respFields.contains(tableDTO.getName()))  continue;
            if(StrUtil.isNotEmpty(tableDTO.getComment())){
                str.append(tableDTO.getName())
                        .append(" AS ")
                        .append("\"").append(tableDTO.getComment()).append("\"")
                        .append(",");
            }else{
                str.append(tableDTO.getName()).append(",");
            }
        }
        return str.substring(0, str.length() - 1);
    }

    /**
     * 映射ExcelC查询的字段，根据配置json
     */
    public static String convertExcelAsFields(String tableName, Object respKey, Map<String, Object> configJson) {
        StringBuilder str = new StringBuilder();
        List<TableDTO> table = tableService.getTable(tableName);
        String respFields = "";
        if(ObjectUtil.isNotEmpty(respKey) && StrUtil.isNotEmpty(respKey.toString())){
            respFields = apiRespRepository.getFieldsByKey(tableName, respKey.toString());
        }
        for (TableDTO tableDTO : table) {
            if(respFields.contains(tableDTO.getName()))  continue;
            String text = getKeyByValue(configJson, (Object) tableDTO.getName());
            if (text != null){
                if(StrUtil.isNotEmpty(text)){
                    str.append(tableDTO.getName())
                            .append(" AS ")
                            .append("\"").append(text).append("\"")
                            .append(",");
                    continue;
                }
            }

            if(StrUtil.isNotEmpty(tableDTO.getComment())){
                str.append(tableDTO.getName())
                        .append(" AS ")
                        .append("\"").append(tableDTO.getComment()).append("\"")
                        .append(",");
                continue;
            }
            str.append(tableDTO.getName()).append(",");

        }
        return str.substring(0, str.length() - 1);
    }
    private static String getKeyByValue(Map<String, Object> map, Object value){
        if (map != null){
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if (entry.getValue().equals(value)) {
                    return entry.getKey();
                }
            }
        }
        return null;
    }

}
